package borge.web.teamspirit.client.widgets;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.Team;
import borge.web.teamspirit.client.data.TeamPlayer;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Label;

public class CreatePlayerWidget extends CreateDataWidget implements ClickHandler {
	
	private Team iTeam;
	private TeamPlayer iPlayer;
    
	public CreatePlayerWidget(WidgetEventHandler aEventHandler, Team aTeam, TeamPlayer aPlayer){
		super(aEventHandler);
	
		iTeam = aTeam;
		iPlayer = aPlayer;
		
		if(aPlayer == null)
		    aPlayer = new TeamPlayer();
		
		
		TeamSpiritConstants lsc = TeamSpirit.getConstants();
		int row = 0;
		final int maxLength = 30;
		iPanel.add(new Label("Create player for team: " + iTeam.getName()));
		
		addTableRow(row++, new InputWrapper(lsc.active(), createCheckBox(aPlayer.isActive()), true));		
		addTableRow(row++, new InputWrapper(lsc.position(), createTextBox(aPlayer.getPosition(), maxLength), false));
		addTableRow(row++, new InputWrapper(lsc.shirt_number(),createTextBox(aPlayer.getShirtNumber(), maxLength), false));
		addTableRow(row++, new InputWrapper(lsc.join_date(), createDateBox(aPlayer.getJoinDate()), false));
		addTableRow(row++, new InputWrapper(lsc.other_info(), createTextArea(aPlayer.getOtherInfo(), maxLength, 5), false));
		
		
		addButton(row, 0, this);
		iPanel.add(iTable);
		initWidget(iPanel);
	}
	
        
    @SuppressWarnings("unchecked")
    public TeamPlayer getDataObject(){
        return iPlayer;
    }
	
	public void onClick(ClickEvent aEvent){
		
	    // Verify input data
	    // * make sure
	    
		postEvent(WidgetEventType.PLAYER_CREATED);
		
	}

}
